package com.bbc.yuntun.platform.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbc.yuntun.common.utils.StringUtils;
import com.bbc.yuntun.platform.domain.WxUser;
import com.bbc.yuntun.platform.mapper.WxUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;


/**
 * 微信用户Service业务层处理
 *
 * @author wl
 * @date 2022-12-01
 */
@Service
public class WxUserService extends ServiceImpl<WxUserMapper, WxUser> {

    /**
     * 查询微信用户
     *
     * @param id 微信用户主键
     * @return 微信用户
     */
    public WxUser selectWxUserById(Long id) {
        return getById(id);
    }

    /**
     * 通过openId查询微信用户
     *
     * @param openId 微信用户openId
     * @author wl
     * @date 2023/12/11 12:17
     * @return com.bbc.yuntun.platform.domain.WxUser
     */
    public WxUser selectWxUserByOpenId(String openId) {
        return baseMapper.selectWxUserByOpenId(openId);
    }

    /**
     * 查询微信用户列表
     *
     * @param wxUser 微信用户
     * @return 微信用户
     */
    public List<WxUser> selectWxUserList(WxUser wxUser) {
        LambdaQueryWrapper<WxUser> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(wxUser.getAvatarUrl())) {
            queryWrapper.eq(WxUser::getAvatarUrl, wxUser.getAvatarUrl());
        }
        if (StringUtils.isNotEmpty(wxUser.getMobile())) {
            queryWrapper.eq(WxUser::getMobile, wxUser.getMobile());
        }
        if (StringUtils.isNotEmpty(wxUser.getOpenId())) {
            queryWrapper.eq(WxUser::getOpenId, wxUser.getOpenId());
        }
        if (StringUtils.isNotEmpty(wxUser.getSessionKey())) {
            queryWrapper.eq(WxUser::getSessionKey, wxUser.getSessionKey());
        }
        if (StringUtils.isNotEmpty(wxUser.getNickName())) {
            queryWrapper.like(WxUser::getNickName, wxUser.getNickName());
        }
        queryWrapper.orderByDesc(WxUser::getId);
        return this.list(queryWrapper);
    }

    /**
     * 新增微信用户
     *
     * @param wxUser 微信用户
     * @return 结果
     */
    public boolean insertWxUser(WxUser wxUser) {
        return save(wxUser);
    }

    /**
     * 修改微信用户
     *
     * @param wxUser 微信用户
     * @return 结果
     */
    public boolean updateWxUser(WxUser wxUser) {
        return updateById(wxUser);
    }

    /**
     * 批量删除微信用户
     *
     * @param ids 需要删除的微信用户主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteWxUserByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids));
    }

}
